package com.idp.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.common.base.BaseController;
import com.idp.common.model.ResultModel;
import com.idp.system.dto.SysButtonDto;
import com.idp.system.dto.SysMenuDto;
import com.idp.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单Controller
 * @author King
 */
@Api(tags="菜单")
@RestController
@RequestMapping("/menu/")
public class SysMenuController extends BaseController {

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 分页查询
	 * @return
	 */
	@ApiOperation("树形列表查询")
	@ApiOperationSupport(includeParameters = {"sysMenuDto.parentId"})
	@PostMapping("findByTreeTable")
	public ResultModel<List<SysMenuDto>> findByTreeTable(@RequestBody SysMenuDto sysMenuDto) {
		return ResultModel.success(sysMenuService.findByTreeTable(sysMenuDto.getParentId()));
	}

	/**
	 * 通过id查询
	 * @param sysMenuDto
	 * @return
	 */
	@ApiOperation("通过id查询")
	@ApiOperationSupport(includeParameters = {"sysMenuDto.id"})
	@PostMapping("getById")
	public ResultModel<SysMenuDto> getById(@RequestBody SysMenuDto sysMenuDto) {
		return ResultModel.success(sysMenuService.getById(sysMenuDto.getId()));
	}

	/**
	 * 新增
	 * @param sysMenuDto
	 * @return
	 */
	@ApiOperation("新增")
	@ApiOperationSupport(ignoreParameters = {"sysMenuDto.id", "sysMenuDto.hasChildren",
			"sysMenuDto.children", "sysMenuDto.buttons"})
	@PostMapping("add")
	public ResultModel add(@RequestBody SysMenuDto sysMenuDto) {
		sysMenuService.add(sysMenuDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改
	 * @param sysMenuDto
	 * @return
	 */
	@ApiOperation("修改")
	@ApiOperationSupport(ignoreParameters = {"sysMenuDto.children", "sysMenuDto.hasChildren", "sysMenuDto.buttons"})
	@PostMapping("update")
	public ResultModel update(@RequestBody SysMenuDto sysMenuDto) {
		sysMenuService.update(sysMenuDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 删除
	 * @param sysMenuDto
	 * @return
	 */
	@ApiOperation("删除")
	@ApiOperationSupport(includeParameters = {"sysMenuDto.id"})
	@PostMapping("delete")
	public ResultModel delete(@RequestBody SysMenuDto sysMenuDto) {
		sysMenuService.delete(sysMenuDto.getId());
		return ResultModel.success("删除成功", null);
	}

	/**
	 * 查询所有菜单
	 * @return
	 */
	@ApiOperation("查询所有菜单")
	@PostMapping("findAllForTree")
	public ResultModel<List<SysMenuDto>> findAllForTree() {
		return ResultModel.success(sysMenuService.findAllForTree());
	}

	/**
	 * 通过菜单id获取菜单按钮
	 * @param sysButtonDto
	 * @return
	 */
	@ApiOperation("通过菜单id获取菜单按钮")
	@ApiOperationSupport(includeParameters = {"sysButtonDto.menuId"})
	@PostMapping("getButtonsByMenuId")
	public ResultModel<SysMenuDto> getButtonsByMenuId(@RequestBody SysButtonDto sysButtonDto) {
		return ResultModel.success(sysMenuService.getButtonsByMenuId(sysButtonDto.getMenuId()));
	}

	/**
	 * 设置菜单下按钮
	 * @param sysMenuDto
	 * @return
	 */
	@ApiOperation("设置菜单下按钮")
	@ApiOperationSupport(ignoreParameters = {"sysMenuDto.children", "sysMenuDto.parentId", "sysMenuDto.hasChildren",
			"sysMenuDto.menuName", "sysMenuDto.menuUrl", "sysMenuDto.menuIcon", "sysMenuDto.sort"})
	@PostMapping("setButtonsByMenuId")
	public ResultModel setButtonsByMenuId(@RequestBody SysMenuDto sysMenuDto) {
		sysMenuService.setButtonsByMenuId(sysMenuDto);
		return ResultModel.success("设置成功", null);
	}

}
